<?php

namespace App\Models\Common;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use DateTimeInterface;
use Illuminate\Support\Arr;

abstract class CommonModel extends Model
{
    use SoftDeletes;
    protected $guarded = [];
    protected $hidden = ['password','deleted_at'];

    protected function serializeDate(DateTimeInterface $date) {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    /**
     * 关联关系取消蛇形命名
     * @param bool $val
     * @return bool
     * @Date 2021/1/9
     * @Author Guodongsheng
     */
    public static function isSnakeAttributes($val = true): bool {
        self::$snakeAttributes = $val;
        return self::$snakeAttributes;
    }

    /*public function __construct(array $attributes = []) {
        parent::__construct($attributes);
        self::isSnakeAttributes(false);

    }*/

    public function getRecords(array $conditions){

        if(!isset($conditions['orderBy'])){
            $conditions['orderBy']['col'] = 'id';
            $conditions['orderBy']['sort'] = 'desc';
        }

        $records = $this->newQuery();
        if(isset($conditions['ids'])){
            if(is_array($conditions['ids'])){
                $conditions['ids'] = array_filter($conditions['ids']);
                //这一层判断必须有，当array_filter是一个空数组
                if(count($conditions['ids'])>0){
                    $records = count($conditions['ids'])>1?$records->whereIn('id',$conditions['ids']):
                        $records->where('id',Arr::first($conditions['ids']));
                }
            }
            else{
                $records = $records->where('id',$conditions['ids']);
            }
        }

        if(!empty($conditions['password'])){
            $records = $records->where('password',$conditions['password']);
        }
        if(!empty($conditions['phone'])){
            $records = $records->where('phone',$conditions['phone']);
        }
        if(!empty($conditions['name'])){
            $records = $records->where('name','like','%'.$conditions['name'].'%');
        }
        if(!empty($conditions['account'])){
            $records = $records->where('nick_name','like','%'.$conditions['account'].'%');
        }
        if(!empty($conditions['createdAt'])){
            $records = $records->whereDate('createdAt',$conditions['createdAt']);
        }
        if(isset($conditions['status']) && $conditions['status']!='all'){
            if(is_array($conditions['status'])){
                $conditions['status'] = array_filter($conditions['status']);
                //这一层判断必须有，当array_filter是一个空数组
                if(count($conditions['status'])>0){
                    $records = count($conditions['status'])>1?$records->whereIn('status',$conditions['status']):
                        $records->where('status',Arr::first($conditions['status']));
                }
            }
            else{
                $records = $records->where('status',$conditions['status']);
            }
        }
        if(isset($conditions['type']) && $conditions['type']!='all'){
            if(is_array($conditions['type'])){
                $conditions['type'] = array_filter($conditions['type']);
                //这一层判断必须有，当array_filter是一个空数组
                if(count($conditions['type'])>0){
                    $records = count($conditions['type'])>1?$records->whereIn('type',$conditions['type']):
                        $records->where('type',Arr::first($conditions['type']));
                }
            }
            else{
                $records = $records->where('type',$conditions['type']);
            }
        }
        return $records->orderBy($conditions['orderBy']['col'],$conditions['orderBy']['sort']);
    }

    public function getRecordsByIds(array $ids,array $column = ['*']) {
        return $this->getRecords(compact('ids'))->get($column);
    }

    public function getRecordById($id,array $column = ['*']) {
        return $this->getRecords(['ids'=>$id])->first($column);
    }

    public function getLst(array $conditions=[]) {
        return $this->getRecords($conditions);
    }
}
